package cn.com.note.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;

import cn.com.note.R;


public class CustomLayoutView extends LinearLayout {
    Context context;
    Drawable bgDrawable;
    int bgDrawableColor;

    public CustomLayoutView(Context context) {
        this(context, null);
    }

    public CustomLayoutView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        this.context = context;
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomLayoutView);
        bgDrawable = array.getDrawable(R.styleable.CustomLayoutView_clv_bgDrawable);
        bgDrawableColor = array.getColor(R.styleable.CustomLayoutView_clv_bgDrawableColor, Color.BLACK);
        array.recycle();
        if (bgDrawable == null) {
            bgDrawable = ContextCompat.getDrawable(context, R.drawable.shape_semicircle_black);
        }
        setBackground(getDrawable(bgDrawableColor));
    }

    private Drawable getDrawable(int color, float radius) {
        GradientDrawable myGrad = (GradientDrawable) bgDrawable;
        myGrad.setCornerRadius(radius);
        myGrad.setColor(color);
        return myGrad;
    }

    private Drawable getDrawable(int color) {
        return getDrawable(color, SizeUtils.dp2px(25));
    }

    public void setBgColor(int color) {
        setBgColor(color, SizeUtils.dp2px(25));
    }

    public void setBgColor(int color, int radios) {
        setBackground(getDrawable(color, radios));
    }
}
